/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.msg.hrappy.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.msg.hrappy.backend.model.Notification;
import edu.msg.hrappy.backend.model.User;
import edu.msg.hrappy.backend.service.NotificationService;
import edu.msg.hrappy.backend.service.UserService;


public class ShowNotificationServlet extends HttpServlet {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@EJB
	private NotificationService notificationService;
	@EJB
	private UserService userService;

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		 HttpSession session = req.getSession();
		 session.setAttribute("er", null);
		User user = userService.getUserByUserName(req.getUserPrincipal()
				.toString());
		session.setAttribute("userId", user.getId());
		session.setAttribute("username", user.getUserName());
		List<Notification> notifications = user.getNotifications();
		List<Notification> notifications2=new ArrayList<Notification>();
		
		for (Notification element : notifications) {
		   if(!element.isSeen()) {
			   notifications2.add(element);
			   Notification n= notificationService.getById(element.getId());
			   n.setSeen(true);
			   notificationService.update(n);
			   
		   }
		   
		}
		
		session.setAttribute("notifications", notifications2);
		
		
		String address = "./showNotifications.jsp";
		RequestDispatcher dispatcher = req.getRequestDispatcher(address);
		dispatcher.forward(req, resp);

	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		
			

	}
}
